//按钮宽度不要超过60px

;(function($){

	$.fn.ajaxfileupload = function(options) {

		var defaults = {
			url: '',
			name: 'file',
			data: {},
			success: null,
			error: null
		};

		var options = $.extend(defaults, options);

		this.each(function(){
			var $this = $(this);
			if ($this.data('ajaxfileupload') === 1) {
				return true;
			}
			
			$this.addClass('ajaxfileupload');

			var id = new Date().getTime();
			var input = $('<input type="file" class="ajaxfileupload_file" name="' + options.name + '" id="' + id + '" size="1" hidefocus="true" />');
			if (window.ActiveXObject) {
				input.css({height:($this.outerHeight() + 150)});
			}
			else if (window.MessageEvent && !document.getBoxObjectFor) {
				input.css({width:$this.outerWidth() + 22, height:($this.outerHeight() + 150)});
			}

			input.change(function(event){
				
				$.ajaxFileUpload({
					url: options.url,
					data: options.data,
					secureuri: false,
					fileElementId: $(this).attr('id'),
					dataType: 'json',
					success: function (data, status) {
						if (typeof(options.success) === 'function') {
							options.success(data, status);
						}
					},
					error: function (data, status, e) {
						if (typeof(options.error) === 'function') {
							options.error(data, status, e);
						}
					}
				});

				$this.data('ajaxfileupload', 0);
				$this.ajaxfileupload(options);

			});

			$this.append(input).data('ajaxfileupload', 1);
		});
	}

})(jQuery);